﻿<%@ WebHandler Language="C#"  Class="MicroWebsiteUpload" %>

    using Chain.BLL;
    using Chain.Model;
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Web;

    public partial class MicroWebsiteUpload : IHttpHandler
    {
        private HttpContext currentContext;

        private void DrawImage(int MerchantID)
        {
            Chain.Model.MerchantSite MerchantSiteModel = new Chain.BLL.MerchantSite().GetModel(MerchantID);
            using (Bitmap smallImage = new Bitmap(this.currentContext.Server.MapPath(MerchantSiteModel.MerchantPhoto)))
            {
                Graphics.FromImage(smallImage);
                this.currentContext.Response.ContentType = "image/jpeg";
                smallImage.Save(this.currentContext.Response.OutputStream, ImageFormat.Jpeg);
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            this.currentContext = context;
            HttpPostedFile sourse = null;
            string savePath = string.Empty;
            string saveName = string.Empty;
            try
            {
                string name19;
                if (context.Request.Files["MicroWebsiteIndex"] != null)
                {
                    Match match = Regex.Match(context.Request["name"], "(/.*/)(.*)");
                    sourse = context.Request.Files["MicroWebsiteIndex"];
                    savePath = match.Groups[1].Value;
                    saveName = match.Groups[2].Value;
                    this.SaveImg(sourse, savePath, saveName);
                    context.Response.Write("1");
                    return;
                }
                if (context.Request.Files["MicroWebsiteProductCenter"] != null)
                {
                    sourse = context.Request.Files["MicroWebsiteProductCenter"];
                    savePath = "~/Upload/MicroWebsite/MicroWebsiteProductCenter/";
                    saveName = DateTime.Now.ToString("yyMMddhhmmssffff") + Path.GetExtension(sourse.FileName);
                    this.SaveImg(sourse, savePath, saveName);
                    context.Response.Write(saveName);
                    return;
                }
                if (context.Request.Files["MicroWebsiteSymbol"] != null)
                {
                    sourse = context.Request.Files["MicroWebsiteSymbol"];
                    savePath = "~/Upload/MicroWebsite/MicroWebsiteSymbol/";
                    saveName = DateTime.Now.ToString("yyMMddhhmmssffff") + Path.GetExtension(sourse.FileName);
                    this.SaveImg(sourse, savePath, saveName);
                    context.Response.Write(saveName);
                    return;
                }
                if (context.Request.Files["MicroGoods"] != null)
                {
                    sourse = context.Request.Files["MicroGoods"];
                    savePath = "~/Upload/MicroWebsite/MicroGoods/";
                    saveName = DateTime.Now.ToString("yyMMddhhmmssffff") + Path.GetExtension(sourse.FileName);
                    this.SaveImg(sourse, savePath, saveName);
                    context.Response.Write(saveName);
                    return;
                }
                if ((context.Request["type"] != null) && (context.Request["type"] == "index"))
                {
                    int MerchantID = Convert.ToInt32(context.Request["MerchantID"]);
                    this.DrawImage(MerchantID);
                    return;
                }
                if (context.Request.Files["WeiXinPhoto"] == null)
                {
                    goto Label_02F5;
                }
                sourse = context.Request.Files["WeiXinPhoto"];
                savePath = "~/Upload/WeiXin/Images/";
                string name = context.Request["name"];
                if (!string.IsNullOrEmpty(name) && ((name19 = name) != null))
                {
                    if (!(name19 == "1"))
                    {
                        if (name19 == "2")
                        {
                            goto Label_02D0;
                        }
                        if (name19 == "3")
                        {
                            goto Label_02D8;
                        }
                    }
                    else
                    {
                        saveName = "bg.jpg";
                    }
                }
                goto Label_02DE;
            Label_02D0:
                saveName = "wwz.jpg";
                goto Label_02DE;
            Label_02D8:
                saveName = "memCard.jpg";
            Label_02DE:
                this.SaveImg(sourse, savePath, saveName);
                context.Response.Write(saveName);
                return;
            Label_02F5:
                context.Response.Write("0");
            }
            catch (Exception e)
            {
                Chain.Model.SysError err = new Chain.Model.SysError();
                err.UserID = 0;
                err.ShopID = 0;
                err.ErrorTime = DateTime.Now;
                err.ErrorContent = e.ToString();
                err.Ipaddress = PubFunction.ipAdress;
                new Chain.BLL.SysError().Add(err);
                throw e;
            }
        }

        private void SaveImg(HttpPostedFile sourse, string savePath, string saveName)
        {
            sourse.SaveAs(this.currentContext.Server.MapPath(savePath) + saveName);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
